package com.nielsenedu.exam202604Midterm;

import java.util.Arrays;

public class AppleCrate {

	private Apple[][] apples;

	public AppleCrate(int rows, int cols) {
		this.apples = new Apple[rows][cols];
	}

	public AppleCrate(Apple[][] apples) {
		this.apples = apples;
	}

	public int getRows() { return apples.length; };
	public int getCols() { return apples[0].length; };
	public void addApple(Apple a, int row, int col) {
		apples[row][col] = a;
	}
	public String toString() {
		String s = "";
		for(Apple[] row : apples) {
			for(Apple a : row) {
				s += a.toString();
			}
			s += "\n";
		}
		return s;
	}

	/**
	 * Returns the number of pies that can be made from the 2D
	 * array apples if each pie requires poundsPerPie pounds of
	 * apples. If an apple is rotten, it cannot be used in a pie,
	 * nor can any adjacent apple.
	 * Preconditions: apples has at least two rows
	 * and at least two columns.
	 * No elements in apples are null.
	 */
	public int numberOfPies(double poundsPerPie) {
		double usable = 0.0;
		for(int row = 0; row < apples.length; row++) {
			for(int col = 0; col < apples[row].length; col++) {
				if(!apples[row][col].isRotten() &&
						(row == 0                    || !apples[row-1][col].isRotten()) &&
						(row == apples.length-1      || !apples[row+1][col].isRotten()) &&
						(col == 0                    || !apples[row][col-1].isRotten()) &&
						(col == apples[row].length-1 || !apples[row][col+1].isRotten())) {
					usable += apples[row][col].getWeight();
				}
			}
		}
		//System.out.println(usable);
		return (int)(usable / poundsPerPie);
	}

	// If you prefer the opposite logic (DeMorgan's of the above)
	public int numberOfPies1(double poundsPerPie) {
		double usable = 0.0;
		for(int row = 0; row < apples.length; row++) {
			for(int col = 0; col < apples[row].length; col++) {
				if(apples[row][col].isRotten() ||
					(row != 0                   && apples[row-1][col].isRotten()) ||
					(row < apples.length-1      && apples[row+1][col].isRotten()) ||
					(col != 0                   && apples[row][col-1].isRotten()) ||
					(col < apples[row].length-1 && apples[row][col+1].isRotten())) {
					;
				} else {
					usable += apples[row][col].getWeight();
				}
			}
		}
		return (int)(usable / poundsPerPie);
	}
	
	// To output some debug information:
	public int numberOfPies2(double poundsPerPie) {
		double usable = 0.0;
		boolean[][] canUse = new boolean[apples.length][apples[0].length];
		for(int row = 0; row < apples.length; row++) {
			for(int col = 0; col < apples[row].length; col++) {
				if(apples[row][col].isRotten() ||
					(row != 0                   && apples[row-1][col].isRotten()) ||
					(row < apples.length-1      && apples[row+1][col].isRotten()) ||
					(col != 0                   && apples[row][col-1].isRotten()) ||
					(col < apples[row].length-1 && apples[row][col+1].isRotten())) {
					canUse[row][col] = false;
				} else {
					usable += apples[row][col].getWeight();
					canUse[row][col] = true;
				}
			}
		}
		for(boolean[] row : canUse) {
			System.out.println(Arrays.toString(row));
		}
		System.out.println(usable);
		return (int)(usable / poundsPerPie);
	}
	
	
	// *** TEST CODE ***

	public static void main(String[] args) {
		AppleCrate crate = new AppleCrate(new Apple[][] {
			{ new Apple(0.3), new Apple(0.4), new Apple(0.35), new Apple(0.5), new Apple(0.45), new Apple() },
			{ new Apple(0.42), new Apple(0.38), new Apple(0.33), new Apple(0.4), new Apple(0.41), new Apple(0.44)},
			{ new Apple(0.32), new Apple(0.44), new Apple(), new Apple(0.49), new Apple(0.29), new Apple(0.32)},
			{ new Apple(0.3), new Apple(0.4), new Apple(0.35), new Apple(0.5), new Apple(0.45), new Apple(0.4)}
		});
		System.out.println(crate);
		System.out.println("Number of pies: " + crate.numberOfPies(2.0));
	}

}
